<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';

if($_RQ['op']=='list'){
	$fields=array('id','mid','star','groupid','isrecommand','salecate','cateid','mobile','realname','status','jointime','accounttime','logo','merchname','upass','uname');
	$params=array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'mid >'=>0);
	if(!empty($_RQ['merchname'])){
		$params['merchname like']='%'.trim($_RQ['merchname']).'%';
		$return['params']['merchname']=$_RQ['merchname'];
	}
	if(!empty($_RQ['realname'])){
		$params['realname like']='%'.trim($_RQ['realname']).'%';
		$return['params']['realname']=$_RQ['realname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=intval($_RQ['mobile']);
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['status']!=''){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['pcd']){
		$pcd=cfc('tools')->base64_decode($_RQ['pcd'],$_SESSION['base_key_str']);
		$pcd=json_decode($pcd,true);
		$return['params']['pcd']=$pcd;
	}
	if($pcd['province']){
		$params['pcd like']='%'.$pcd['province'].'%';
	}
	if($pcd['city']){
		$params['pcd like']='%'.$pcd['city'].'%';
	}
	if($pcd['district']){
		$params['pcd like']='%'.$pcd['district'].'%';
	}
	if($pcd['street']){
		$params['pcd like']='%'.$pcd['street'].'%';
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$orderby='star desc, id desc';
	$total='';
	$return['list']=pdo_getslice('jy_merch_user',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	foreach($return['list'] as $k=>&$v){
		$v['jointime']=date('Y/m/d H:i:s',$v['jointime']);
		$v['url']=custom_url('jy_xiaoemerch/index//merchid='.$v['id'],true);
		$v['appurl']='jy_xiaoemerch/index//merchid='.$v['id'];
		$v['member']=cfc('core')->member_show($v['mid']);
		$v['catename']=pdo_getcolumn('jy_merch_category',array('id'=>$v['cateid'],'uniacid'=>$_SESSION['uniacid']),'catename');
		$v['total']=pdo_count('jy_xiaoeknow_user',array('isvip'=>1,'merchid'=>$v['id'],'uniacid'=>$_SESSION['uniacid']));
		$v['fee1']=pdo_getsum('jy_xiaoeknow_viporder',array('status'=>1,'merchid'=>$v['id'],'uniacid'=>$_SESSION['uniacid']),'fee');
		$v['fee2']=pdo_getsum('jy_xiaoeknow_video_order',array('ispay'=>1,'merchid'=>$v['id'],'uniacid'=>$_SESSION['uniacid']),'fee');
		$v['fee']=$v['fee1']+$v['fee2'];
	}
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：套餐权限设置--支持小鹅多商户版--多商户登录PC后台后，应用里面才会显示出来！','delall'=>false,'del_one'=>false,'edit'=>false,'list'=>array(
			array('title'=>'商户信息_Merch','type'=>'img_string','img'=>'logo',
			'data'=>array(
				array('title'=>'名称：','name'=>'merchname'),
				array('title'=>'项目：','name'=>'salecate')
				)
			),
			array('name'=>'groupid','title'=>'商户等级','type'=>'select','options'=>'group','option_title'=>'groupname'),
			array('title'=>'分类/时间','type'=>'img_string',
			'data'=>array(
				array('title'=>'分类：','name'=>'catename'),
				array('title'=>'入驻：','name'=>'jointime')
				)
			),
			array('name'=>'url','title'=>'小鹅入口','type'=>'ewm','action'=>'appurl'),	
			array('name'=>'url','title'=>'小鹅地址','type'=>'copy','action'=>'appurl'),	
			array('name'=>'total','title'=>'VIP人数','type'=>'string'),
			array('name'=>'fee1','title'=>'VIP金额','type'=>'string'),
			array('name'=>'fee2','title'=>'买课金额','type'=>'string'),
			array('name'=>'fee','title'=>'总付费金额','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'setgroup','title'=>'设置商户等级'),
			array('type'=>'modal','op'=>'setauth','title'=>'设置套餐权限'),
		)),
		'search'=>array(
			array('name'=>'merchname','width'=>8,'title'=>'商户名称','type'=>'string'),
			array('name'=>'realname','width'=>8,'title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','width'=>8,'title'=>'电话','type'=>'string'),
			array('name'=>'status','width'=>8,'title'=>'状态','type'=>'select','options'=>'status'),
			array('type'=>'pcd2','width'=>'40','title'=>'区域','name'=>'pcd','help'=>''),
		)
	);
	$return['status2']=plugin('jy_merch')->merch_status;
	$return['group']=pdo_getall('jy_merch_group',array('uniacid'=>$_SESSION['uniacid']),array('id','groupname'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setgroup'){
	if($_RQ['postdata']){
		$post=get_postdata();
		pdo_update('jy_merch_user',array('groupid'=>$post['groupid']),array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
        $this->model('model')->upmember($post['groupid'],$post['mid']);
		exi('修改成功！','','list');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_merch_user',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','mid','groupid'));
	if(!$item){
		exi('指定商户不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改商户等级',
		'edit'=>array(
			array('name'=>'groupid','title'=>'商户等级','type'=>'select','options'=>'group','option_title'=>'groupname'),	
		),
	);
	$return['group']=pdo_getall('jy_merch_group',array('uniacid'=>$_SESSION['uniacid']),array('id','groupname'),'id');
	exi($return);
}
if($_RQ['op']=='setauth'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$post['pluginset']['plugins']=$post['plugins'];
		$post['pluginset']['games']=$post['games'];
		pdo_update('jy_merch_user',array('pluginset'=>ijson_encode($post['pluginset'])),array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
		exi('修改成功！','','list');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_merch_user',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','mid','pluginset'));
	if(!$item){
		exi('指定商户不存在！','error');
	}
	$item['pluginset']=$item['pluginset']?json_decode($item['pluginset'],true):array('isopen'=>0);
	$item['plugins']=$item['pluginset']['plugins'];
	$item['games']=$item['pluginset']['games'];
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'套餐权限',
		'edit'=>array(
			array('title'=>'这里的设置优先，商户分组里面的《套餐权限》的设置','type'=>'tip'),
			array('type'=>'checkbox','title'=>'功能权限','name'=>'plugins','options'=>'plugins','help'=>'设置商户的功能权限（<red>如果不显示，说明暂时不需要设置</red>）'),
			array('type'=>'checkbox','title'=>'活动权限','name'=>'games','options'=>'games','help'=>'设置商户的活动功能权限（<red>如果不显示，说明暂时不需要设置</red>）'),
		),
	);
	$return['plugins']=plugin('jy_merch')->model('model')->get_plugins();
	$return['games']=plugin('jy_merch')->model('model')->get_games();
	exi($return);
}